/*
 * PreviewDialogBox
 *
 * Preview in a dialog box
 */

package net.kernub.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.Window;

public class PreviewDialogBox
{
	private final wwwConstants constants = GWT.create( wwwConstants.class );

	private final DialogBox dialogBox = new DialogBox();

	PreviewDialogBox( String title, String url )
	{
		int clientHeight = Window.getClientHeight();
		int clientWidth = Window.getClientWidth();

		String frameWidth = ((clientWidth/3)*2 - 10) + "px";
		String frameHeight = ((clientHeight/3)*2 - 10) + "px";
		String dialogWidth = ((clientWidth/3)*2) + "px";
		String dialogHeight = ((clientHeight/3)*2) + "px";

		frameWidth = (Window.getClientWidth() - 50) + "px";

		final Frame previewFrame = new Frame();
		previewFrame.addStyleName( "Preview_Frame" );
		previewFrame.setUrl( url );
		previewFrame.setWidth( frameWidth );
		previewFrame.setHeight( frameHeight );

		final Button closeButton = new Button( constants.ok() );
		closeButton.addStyleName( "Preview_Button" );
		closeButton.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				dialogBox.hide();
			}
		});

		final VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add( previewFrame );
		mainPanel.setCellHorizontalAlignment( previewFrame, VerticalPanel.ALIGN_CENTER );
		mainPanel.add( closeButton );
		mainPanel.setCellHorizontalAlignment( closeButton, VerticalPanel.ALIGN_CENTER );

		dialogBox.setText( title );
		dialogBox.setAnimationEnabled( false );
		dialogBox.setWidget( mainPanel );
	
		dialogBox.setWidth( "100%" );
		dialogBox.setHeight( "70%" );

		dialogBox.setPopupPositionAndShow( new PopupPanel.PositionCallback() {
			public void setPosition( int offsetWidth, int offsetHeight )
			{
				// TODO: weired! should have a look!
				//GWT.log( "offsetWidth: " + offsetWidth + " offsetHeight: " + offsetHeight, null );
				int left = (Window.getClientWidth() - offsetWidth) / 3;
				int top = (Window.getClientHeight() - offsetHeight) / 3;
				dialogBox.setPopupPosition( left, top );
			}
		});
	}
}

